import {
    createRouter,
    createWebHashHistory,
    RouteRecordRaw
} from 'vue-router'
import {useUserStore} from "../store/user";
import { useTitle } from '@vueuse/core'
let routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/home/login/index.vue'),
        meta: {
            title: ''
        }
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('@/views/home/login/register.vue'),
        meta: {
            title: ''
        }
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/index.vue'),
        meta: {
            title: ''
        }
    },
    {
        path: '/zhuanye',
        name: 'zhuanye',
        component: () => import('@/views/home/zhuanye.vue'),
        meta: {
            title: ''
        }
    },
    {
        path: '/gj',
        name: 'gj',
        component: () => import('@/views/home/gj.vue'),
        meta: {
            title: ''
        }
    },
    {
        path: '/pc',
        name: 'gpcj',
        component: () => import('@/views/home/pc/index.vue'),
        meta: {
            title: ''
        }
    },
    {
        path: '/start',
        name: 'start',
        component: () => import('@/views/home/pc/start.vue'),
        meta: {
            title: ''
        }
    },
    {
        path: '/result',
        name: 'result',
        component: () => import('@/views/home/pc/result.vue'),
        meta: {
            title: ''
        }
    },
   
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((async (to, from, next) => {
    const title = useTitle()
    title.value = to.meta.title as string || '儿童青少年心理保健家校行'
    const user = useUserStore()
    const pathWithList = ['/zhuanye']
    console.log(to);
    if (!user?.token ) {
        if (pathWithList.includes(to.path)) {
            next({replace: true, path: '/login'})
        }
    }
    next()
}))


router.afterEach(() => {
  
})



export default router


